package com.wl.boot.batch.mapper;

import com.wl.boot.batch.bean.entity.CommonExcelConfig;
import com.wl.boot.batch.bean.entity.CommonExcelPageField;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface excelMapper {

    @Select({"<script>", " select config_id,excel_id,sheet_index,start_row,target_table from common_excel_config where excel_id = #{excelId}", "</script>"})
    @Results(
            {
                    @Result(id = true, column = "config_id", property = "configId"),
                    @Result(column = "excel_id", property = "excelId"),
                    @Result(column = "sheet_index", property = "sheetIndex"),
                    @Result(column = "start_row", property = "startRow"),
                    @Result(column = "target_table", property = "targetTable")
            }
    )
    CommonExcelConfig getExcelConfigByExcelId(String excelId);

    @Select({"<script>", " select page_id,seq_no,col_name,field_name,field_data_type from common_excel_page_field where page_id = #{configId} order by seq_no", "</script>"})
    @Results(
            {
                    @Result(id = true, column = "page_id", property = "pageId"),
                    @Result(column = "seq_no", property = "seqNo"),
                    @Result(column = "col_name", property = "colName"),
                    @Result(column = "field_name", property = "fieldName"),
                    @Result(column = "field_data_type", property = "fieldDataType")
            }
    )
    List<CommonExcelPageField> getExcelPageFieldByExcelId(String pageId);

    @Insert({"<script>", "  insert into ${tableName} " +
            " <foreach collection=\"tableCode\" item=\"item\" separator =\",\" open=\"( data_id,\" close=\")\"> ${item.fieldName} </foreach> " +
            " values " +
            " <foreach collection=\"datas\" item=\"ditem\" separator =\",\"> " +
            " <foreach collection=\"ditem\" item=\"temp\" open=\"( uuid(),\" close=\")\" separator =\",\"> #{temp,jdbcType=VARCHAR} " +
            "</foreach> </foreach> ",
            "</script>"})
    void batchTargetTable(@Param("tableName") String tableName, @Param("datas") List datas, @Param("tableCode") List<CommonExcelPageField> fields);


    @Insert({"<script>", "  insert into ${tableName} " +
            " <foreach collection=\"tableCode\" item=\"item\" separator =\",\" open=\"( data_id,\" close=\")\"> ${item.fieldName} </foreach> " +
            " values " +
            " <foreach collection=\"datas\" item=\"temp\" open=\"( uuid(),\" close=\")\" separator =\",\"> #{temp,jdbcType=VARCHAR} " +
            "</foreach> ",
            "</script>"})
    void InsertTargetTable(@Param("tableName") String tableName, @Param("datas") List datas, @Param("tableCode") List<CommonExcelPageField> fields);
}
